package com.library.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.base.BaseController;
import com.library.base.R;
import com.library.domain.pojo.Book;
import com.library.service.BookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : 凉小天
 * @date : 2021/6/30 16:51
 */
@RestController
@RequestMapping("/book")
public class BookController extends BaseController {

    @Resource
    private BookService bookService;

    /**
     * 分页查询所有数据
     */
    @GetMapping("/list")
    public R selectAll(Page<Book> page,Book book){
        return result(this.bookService.page(page,book));
    }
    /**
     * 添加图书
     */
    @PostMapping("/add")
    public R addBook(@RequestBody Book book){
        return result(bookService.save(book));
    }
    /**
     * 修改图书
     */
    @PostMapping("/update")
    public R updateBook(@RequestBody Book book){
        return result(bookService.updateById(book));
    }
    /**
     * 根据图书ID批量删除图书
     */
    @GetMapping("/delete")
    public R deleteBook(@RequestParam List<String> isbns){
        return result(bookService.removeByIds(isbns));
    }

    //==================读者操作接口=============================

    /**
     * 借阅图书
     */

    @GetMapping("/lendBook")
    public R lendBook(String isbn,Long readerId){
        return result(bookService.lendBook(isbn,readerId));
    }
    /**
     * 归还图书
     */
    @GetMapping("/returnBook")
    public R returnBook(String isbn,Long readerId){
        return result(bookService.returnBook(isbn,readerId));
    }
}